﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace Simple_ToDo.Phone
{
    public class TaskControl
    {
        List<Task> taskList      = new List<Task>();
        List<string> projectList = new List<string>();
        List<string> noteList    = new List<string>();

        public enum Categories { Inbox = 0, Today, Next, Someday, Waiting, Projects, Notes};

        public bool AddTask(string content, DateTime startDate, DateTime endDate, string project = "", Categories category = Categories.Inbox, Task.Priorities priority = Task.Priorities.nothing, string note = "")
        {
            if ((content != "") && (content != null)) 
            {
                if ((project != "") && (project != null))
                {
                    bool coincidence = false;

                    foreach (string s in projectList)
                        if (s == project)
                        {
                            coincidence = true;
                            break;
                        }
                    if (coincidence == false)
                    {
                        projectList.Add(project);
                    }
                }

                if ((category == Categories.Notes) || (category == Categories.Projects))
                    category = Categories.Inbox;

                taskList.Add(new Task(Guid.NewGuid(), content, project, category, priority, note, startDate, endDate));
                return true;
            }
            else
                return false;
        }
        public void RemoveTask(Task t)
        {
            if (taskList.Contains(t))
                taskList.Remove(t);
        }

        public void AddProject(string project)
        {
            bool coincidence = false;
            foreach (string s in projectList)
                if (s == project)
                {
                    coincidence = true;
                    break;
                }

            if (coincidence == false)
            {
                projectList.Add(project);
            }
        }
        public void RemoveProject(string project)
        {
            foreach (Task t in taskList)
                if (t.Project == project)
                    t.Project = "";

            if (projectList.Contains(project))
                projectList.Remove(project);
        }
        public void RemoveProjectWithTasks(string project)
        {
            int count = taskList.Count;

            for (int i = 0; i < count; i++)
            {
                if (taskList[i].Project == project)
                {
                    taskList.RemoveAt(i);
                    i--;
                }
                count = taskList.Count;
            }

            if (projectList.Contains(project))
                projectList.Remove(project);
        }

        public void AddNote(string note)
        {
            noteList.Add(note);
        }
        public void RemoveNote(string note)
        {
            if (noteList.Contains(note))
                noteList.Remove(note);
        }

        public List<Task> TaskList
        {
            get { return taskList; }
        }
        public List<string> ProjectList
        {
            get { return projectList; }
        }
        public List<string> NoteList
        {
            get { return noteList; }
        }

        public void SerializeToXml()
        {
            SaveAndSync.Save(this);
        }
        public void DeserializeFromXml()
        {
            taskList = SaveAndSync.Load().taskList;
            projectList = SaveAndSync.Load().projectList;
            noteList = SaveAndSync.Load().noteList;
        }
        public void Clear()
        {
            taskList.Clear();
            projectList.Clear();
            noteList.Clear();
        }
    }

    public class Task
    {
        #region Variables
        private Guid taskID;
        private string content;
        private string note;
        private string project;
        private TaskControl.Categories category;
        private DateTime startDate;
        private DateTime endDate;
        
        public enum Priorities { nothing = 0, low, medium, high};
        private Priorities priority;
        #endregion

        #region Get/Set
        public Guid TaskID
        {
            get { return taskID; }
        }

        public string Content
        {
            get { return content; }
            set { content = value; }
        }

        public string Note
        {
            get { return note; }
            set { note = value; }
        }

        public string Project
        {
            get { return project; }
            set { project = value; }
        }

        public TaskControl.Categories Category
        {
            get { return category; }
            set { category = value; }
        }

        public DateTime StartDate
        {
            get { return startDate; }
            set { startDate = value; }
        }

        public DateTime EndDate
        {
            get { return endDate; }
            set { endDate = value; }
        }

        public Priorities Priority
        {
            get { return priority; }
            set { priority = value; }
        }
        #endregion

        public Task()
        {}

        public Task(Guid _taskID, string _content, string _project, TaskControl.Categories _category, Priorities _priority, string _note, DateTime _startDate, DateTime _endDate)
        {
            this.taskID = _taskID;
            this.content  = _content;
            this.project = _project;
            this.category = _category;
            this.priority = _priority;
            this.note = _note;
            this.startDate = _startDate;
            this.endDate = _endDate;
        }
    }
}